package com.vhly.tuite.controller;

import android.os.AsyncTask;
import com.vhly.tuite.twclient.client.TwRequest;
import com.vhly.tuite.twclient.client.TwResponse;
import com.vhly.tuite.twclient.client.TwitterClient;
import com.vhly.tuite.twclient.client.TwitterParser;
import org.w3c.dom.Element;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2010-7-27
 * Time: 17:05:01
 *
 * @author vhly[FR]
 */

/**
 * AsyncTask Twitter Twitter Task, AsyncTask  &lt;Params, Progress, Result&gt;
 */
public final class TwitterAsyncTask extends AsyncTask<TwRequest,Integer, TwResponse> {

    private TwitterClient client;
    private TwitterClientDelegate delegate;

    /**
     * create
     * @param c TwitterClient ִTwitter API Call client
     * @param d TwitterClientDelegate
     */
    public TwitterAsyncTask(TwitterClient c,TwitterClientDelegate d) {
        client = c;
        delegate = d;
    }

    @Override
    protected TwResponse doInBackground(TwRequest... objects) {
        int len = objects.length;
        TwResponse ret = null;
        if(len > 0){
            TwRequest req = objects[0];
            ret = TwitterClient.invokeTW(req);
        }
        return ret;
    }


    @Override
    protected void onPostExecute(TwResponse result) {
        if(result != null){
            String xml = TwitterClient.processResponseData(result);
            if(xml != null){
                Element element = TwitterParser.parseXML(xml);
                if(delegate != null){
                    delegate.process(element);
                }
            }
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }


    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
}
